package config

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql" // 需要引入驱动执行init方法
	"time"
)

var DB *sql.DB

func config() {
	dsn := "root:123456@tcp(127.0.0.1:3306)/mxshop_userop_srv?charset=utf8mb4&parseTime=True&loc=Local"
	var err error
	DB, err = sql.Open("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer DB.Close()
	// 调用ping才是去连接
	if err := DB.Ping(); err != nil {
		fmt.Println("connect 2 database failed")
		panic(err)
	}
	DB.SetConnMaxLifetime(time.Minute * 3)
	DB.SetMaxOpenConns(10)
	DB.SetMaxIdleConns(10)
}
